// pages/pk/result/result.js
const db = wx.cloud.database();
const app=getApp();
var _openid;
var _openidAdd;
var newTotalScore
var addTotalScore
var roomid
var nowPlayer
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result:'',
    crown:0,
    fontColor:'black',
    newPlayerName:'',
    addPlayerName:'',
    newPlayerAvatarUrl:'',
    addPlayerAvatarUrl:'',
    newTotalScore:0,
    addTotalScore:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    roomid=options._id
    console.log(roomid)
    nowPlayer=options.nowPlayer
    //获取本局游戏信息
    db.collection('pkroom').doc(roomid).get().then(res => {
      console.log(res)
      _openid=res.data._openid;
      console.log(_openid)
      _openidAdd=res.data._openidAdd
      newTotalScore=res.data.newTotalScore
      addTotalScore=res.data.addTotalScore
      this.setData({
        newTotalScore:newTotalScore,
        addTotalScore:addTotalScore
      })
      //判断输赢
      if(newTotalScore > addTotalScore){
        console.log('房主赢了')
        if(nowPlayer=='newPlayer'){
          this.setData({
            result:'恭喜你，胜利了',
            crown:1,
            fontColor:'red'
          })
        }else{
          this.setData({
            result:'很遗憾，你输了',
          })
        }
      }else if(newTotalScore < addTotalScore){
        console.log('玩家2赢了')
        if(nowPlayer=='addPlayer'){
          this.setData({
            result:'恭喜你，胜利了',
            crown:1,
            fontColor:'red'
          })
        }else{
          this.setData({
            result:'很遗憾，你输了',
          })
        }
      }else{
        console.log('平局')
        this.setData({
          result:'　　平　局 ',
          crown:1,
          fontColor:'red'
        })
      }
      //获取房主信息
      db.collection('ddata').doc(_openid).get().then(res => {//options._openid
        this.setData({
          newPlayerName:res.data.userInfo.nickName,
          newPlayerAvatarUrl:res.data.userInfo.avatarUrl,
        })
      })
      //获取玩家2信息
      db.collection('ddata').doc(_openidAdd).get().then(res => {//options._openidAdd
        this.setData({
          addPlayerName:res.data.userInfo.nickName,
          addPlayerAvatarUrl:res.data.userInfo.avatarUrl,
        })
      })
    })
    //修改数据库的finished为1（finished为1的相当于完成了所有pk环节），只有房主才能修改
    db.collection('pkroom').doc(roomid).update({
      data: {
        finished: 1
      },
      success: function(res) {
        console.log('本次比赛所有进程均已结束')
      }
    })
  },
  onceAagin:function(){
    wx.switchTab({
      url: '../pk',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})